package com.hanbang.taitaigou.ui.widget.banner;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sai on 15/7/29.
 */
public class CBPageAdapter<T> extends PagerAdapter {
    private Context context;
    protected List<T> mDatas;
    private List<Holder> mHolder = new ArrayList<>();
    protected CBViewHolderCreator holderCreator;
    private boolean canLoop = true;
    private CBLoopViewPager viewPager;
    private int POSITION_CHANG = POSITION_UNCHANGED;


    @Override
    public int getCount() {
        return mHolder == null ? 0 : mHolder.size();
    }

    public int getDataCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public int toRealPosition(int p) {
        if (canLoop) {
            if (getCount() > 1) {
                if (p == 0) {
                    return mDatas.size() - 1;
                } else if (p == getCount() - 1) {
                    return 0;
                } else {
                    return p - 1;
                }
            } else {
                return p;
            }
        } else {
            return p;
        }
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        if(onItemClickListener != null) view.setOnClickListener(onItemClickListener);
        Holder holder = mHolder.get(position);
        holder.UpdateUI(context, toRealPosition(position), mDatas.get(toRealPosition(position)));
        container.addView(holder.getView());
        return holder.getView();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mHolder.get(position).getView());
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public void setCanLoop(boolean canLoop) {
        this.canLoop = canLoop;
    }

    public boolean isCanLoop() {
        return canLoop;
    }

    public void setViewPager(CBLoopViewPager viewPager) {
        this.viewPager = viewPager;
    }

    public CBPageAdapter(Context context, CBViewHolderCreator holderCreator, List<T> datas) {
        this.context = context;
        this.holderCreator = holderCreator;
        this.mDatas = datas;
        mHolder.clear();
        for (int i = 0; i < datas.size(); i++) {
            mHolder.add(getHolder());
        }
    }

    public Holder getHolder() {
        Holder holder = (Holder) holderCreator.createHolder();
        holder.createView(context);
        return holder;
    }


    @Override
    public void notifyDataSetChanged() {
        POSITION_CHANG = POSITION_NONE;
        mHolder.clear();
        for (int i = 0; i < mDatas.size(); i++) {
            Holder holder = getHolder();
            holder.UpdateUI(context, i, mDatas.get(i));
            mHolder.add(holder);
        }
        if (canLoop && mDatas.size() > 1) {
            if (mDatas.size() > 1) {
                Holder holder = getHolder();
                holder.UpdateUI(context, mDatas.size() - 1, mDatas.get(mDatas.size() - 1));
                mHolder.add(0, holder);
                Holder holder2 = getHolder();
                holder2.UpdateUI(context, 0, mDatas.get(0));
                mHolder.add(holder2);
            }
        }
        super.notifyDataSetChanged();
        if (isCanLoop()) {
            if (getCount() > 1) {
                viewPager.setCurrentItem(1, false);
            } else {
                viewPager.setCurrentItem(0, false);
            }
        } else if (getCount() > 0) {
            viewPager.setCurrentItem(0, false);
        }
    }

    /**
     * 意思是如果item的位置如果没有发生变化，则返回POSITION_UNCHANGED。
     * 如果返回了POSITION_NONE，表示该位置的item已经不存在了。
     * 默认的实现是假设item的位置永远不会发生变化，而返回POSITION_UNCHANGED
     *
     * @param object
     * @return
     */
    @Override
    public int getItemPosition(Object object) {
        if (POSITION_CHANG == POSITION_NONE) {
            POSITION_CHANG = POSITION_UNCHANGED;
            return POSITION_NONE;
        } else {
            return POSITION_UNCHANGED;
        }
    }
}
